package concurrent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @author sjw
 * @className Collection
 * @date 2022/4/23 11:42
 * @description: TODO
 */
public class Collection {
    public static void main(String[] args) {

        //fors();
//        delete_for();
        delete_foreach1();
    }

    private static void fors(){
        String[] strings=new String[]{"1","2","3"};
        for(String s:strings){
            System.out.println(s);
        }

        List<String> list= Arrays.asList("1","2","3");
        for(String i:list){
            System.out.println(i);
        }
        System.out.println(list);
    }
    private static void delete_for(){
        ArrayList<String> list= new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        System.out.println(list);
        for(int i=0; i<list.size();i++){
            System.out.println(i+" "+list.size());
            list.remove("2");
        }
        System.out.println(list);
    }

    private static void delete_foreach(){
        ArrayList<String> list= new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.forEach(System.out::println);
        System.out.println(list);
        for(String i: list){
            list.remove("2");
        }
        System.out.println(list);
    }

    private static void delete_foreach1(){
        ArrayList<String> list= new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        System.out.println(list);
        Iterator it = list.iterator();

        while(it.hasNext()) {
            String i = (String)it.next();
            System.out.println(i);
            if(i.equals("2")){
//                it.remove();
                list.remove("2");
            }
        }

        System.out.println(list);
    }
}
